//
//  MCChangeStoreViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCTicketViewControllerClosure = (UIViewController) -> ()


class MCTicketViewController: MCViewController {
    
    public var closure : MCTicketViewControllerClosure?

    
    public var activityType = 0 // 类型(0.全部 2.福利社 3体验4拼团5秒杀6预售7促销活动8社区分销9优惠券)
    private var pageNumber = 1
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sendNetworking()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
    }

    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCTicketViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return (MCScreenWidth-30)*3/7 + 15;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCTicketCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.row = indexPath.row
        cell.delegate = self
             cell.model = self.dataArrayM[indexPath.row] as! MCTicketModel
        return cell
    }
}


extension MCTicketViewController : MCTicketCellDelegate {
    func MCTicketCellDelete(row: Int) {
        
        MCAlterController_twoOptions(vc: self, message: "被领取的优惠券也会失效,确定删除？", confirm: {
            let model = self.dataArrayM[row] as! MCTicketModel
            
            let para = [
                "promotionDetailId" : model.promotionDetailId,
                ] as [String : Any]
            
            MCNetwork_post(url: JAVA_delPromotionDetail, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
                MCHUD_text(message: "删除成功", view: self.view, delay: 1.2)
                
                self.dataArrayM.removeObject(at: row)
                self.tableView.deleteRows(at: [IndexPath.init(row: row, section: 0)], with: UITableViewRowAnimation.automatic)
                
                
                if self.dataArrayM.count == 0 {
                    self.tableView.showBgImage()
                } else {
                    self.tableView.hideBgImage()
                }

            }, dislike: { (response) in }) { (error) in }
            }, cancle: {
                
            })
    }
    
    func MCTicketCellPushto(viewController: UIViewController) {
        closure?(viewController)
    }
}

//MARK: 通知回调，闭包回调，点击事件
extension MCTicketViewController {
    @objc func addButtonClicked() {
        let vc = MCAddTicketViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}


//MARK: 网络请求
extension MCTicketViewController {
    func sendNetworking() {
        
        let para = [
            "activityType" : activityType,
            "pageNum" : pageNumber
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_findActivityInfoListByPage, para, vc: self, success: { (data) in
           
            let dataArray = data.arrayObject ?? []
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            
            if dataArray.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in dataArray {
                    let model = MCTicketModel()
                    model.setValuesForKeys(dict as? [String:Any] ?? [:])
                    self.dataArrayM.add(model)
                }
                
                self.tableView.reloadData()
            }

            
        }, failure: nil)
    }
}

//MARK: UI的处理,通知的接收
extension MCTicketViewController {
    func baseSetting() {
        self.title = "我的优惠券"
        self.view.backgroundColor = UIColor.MCGray_light
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_add_black"), target: self, selector: #selector(addButtonClicked))
        
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }

}


